﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;
using System.Web;

namespace rtw
{
    class fetcher
    {

      static  public HttpWebResponse get(string uri, ref CookieCollection cookies)
        {

            HttpWebRequest wRequest = (HttpWebRequest)WebRequest.Create(uri);
            wRequest.CookieContainer = new CookieContainer();
            wRequest.Method = "GET";
            //wRequest.CookieContainer.Add(cookies);
            HttpWebResponse resp = (HttpWebResponse)wRequest.GetResponse();
            if (resp == null)
            {
                throw new WebException("HttpWebResponse is null!");
            }
            return resp;
        }

      static public string executeGETRequest(string url, ref CookieCollection cookies,
                                        params string[] parameters)
        {
            string _parameters = createParamString(parameters);

            string uri = url + _parameters;
            HttpWebRequest wRequest = (HttpWebRequest)WebRequest.Create(uri);

            int pos = uri.LastIndexOf("/");
            string log_url = uri.Substring(pos, uri.Length - pos);

            //Log.write2Log("GET request:" + log_url);

            wRequest.KeepAlive = true;
            wRequest.CookieContainer = new CookieContainer();

            wRequest.Method = "GET";

            wRequest.CookieContainer = new CookieContainer();
            if (cookies != null)
            {
                wRequest.CookieContainer.Add(cookies);
            }

            HttpWebResponse resp = (HttpWebResponse)wRequest.GetResponse();
            if (resp == null)
            {
                throw new WebException("HttpWebResponse is null.");
            }

            using (HttpWebResponse httpWebResponse = (HttpWebResponse)wRequest.GetResponse())
            {
                string answer = getAnswer(httpWebResponse);
                httpWebResponse.Cookies = wRequest.CookieContainer.GetCookies(wRequest.RequestUri);
                cookies = httpWebResponse.Cookies;
                httpWebResponse.Close();
                if (answer.Length == 0)
                {
                    throw new WebException("Zero length answer.");
                }
                return answer;
            }
        }
    public  static string getAnswer(HttpWebResponse response)
        {
            string c = "utf-8";
            // Obtain a 'Stream' object associated with the response object.
            Stream receiveStream = response.GetResponseStream();
            Encoding encode = System.Text.Encoding.GetEncoding(c);
            StreamReader readStream = new StreamReader(receiveStream, encode);
            string s = readStream.ReadToEnd();
            return s;
        }

      static string createParamString(params string[] vals)
        {
            string str = "";

            if (vals.Length > 0)
            {
                str += "?";
            }

            for (int i = 0; i < vals.Length; i++)
            {
                if (i % 2 == 0)
                {
                    if (i != 0)
                    {
                        str += "&";
                    }
                    str += (vals[i] + "=");
                }
                else
                {
                    str += vals[i];
                }
            }

            return str;
        }

    }
}
